package com.controller;

import com.beans.Article;
import com.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private AccountMapper accountMapper;

    @RequestMapping("/user/login")
    public String login(@RequestParam("userName") String username, @RequestParam("password") String password, Model model, HttpSession session) {
        if (accountMapper.queryAccountByUsername(username) != null && password.equals(accountMapper.queryAccountByUsername(username).getPassword())) {
            session.setAttribute("LoginUser", username);
            // 登录成功，重定向路径/user/articles到test.html页面
            return "redirect:/user/main";
        } else {
            model.addAttribute("msg", "用户名或者密码错误");
            return "login";
        }
    }
    @RequestMapping("/user/login2")
    public String teacherlogin(@RequestParam("userName") String username, @RequestParam("password") String password, Model model, HttpSession session) {
        if (accountMapper.queryAccountByUsername(username) != null && password.equals(accountMapper.queryAccountByUsername(username).getPassword())) {
            session.setAttribute("LoginUser", username);
            // 登录成功，重定向路径/user/articles到test.html页面
            return "redirect:/user/articles";
        } else {
            model.addAttribute("msg", "用户名或者密码错误");
            return "login";
        }
    }
    @RequestMapping("/teacher-login")
    public String teacherLogin() {
        return "teacher-login"; // 返回逻辑视图名称
    }
    @RequestMapping("/student-login")
    public String studentLogin() {
        return "login"; // 返回逻辑视图名称
    }
    @RequestMapping("/user/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index.html";
    }
    @RequestMapping("/gotoSignUp")
    public String gotoSignUp() {
        return "register";
    }
}

/*
    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session) {
        //如果用户名和密码正确
        if ("admin".equals(username) && "123456".equals(password)){
            session.setAttribute("LoginUser", username);//用户名传到session，用于安全控制
            return "dashboard";//跳转到dashboard页面

        }
            //如果用户名或者密码不正确
        else {
            model.addAttribute("msg", "用户名或者密码错误");//显示错误信息
            return "index";//跳转到首页
        }
    }
 */




